<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
 <link href="assets/css/bootstrap.min.css" rel="stylesheet" />
        <link rel="stylesheet" href="css/style.css"/>
        <link rel="stylesheet" href="assets/css/ace.min.css" />
        <link rel="stylesheet" href="assets/css/font-awesome.min.css" />
        <link href="Widget/icheck/icheck.css" rel="stylesheet" type="text/css" />
		<!--[if IE 7]>
		  <link rel="stylesheet" href="assets/css/font-awesome-ie7.min.css" />
		<![endif]-->
        <!--[if lte IE 8]>
		  <link rel="stylesheet" href="assets/css/ace-ie.min.css" />
		<![endif]-->
	    <script src="js/jquery-1.9.1.min.js"></script>
        <script src="assets/js/bootstrap.min.js"></script>
<title>添加产品分类</title>
</head>
<body>
<div class="type_style">
 <div class="type_title">产品类型信息</div>
  <div class="type_content">
  <div class="Operate_btn">
  <a href="javascript:;" class="btn  btn-warning"><i class="icon-edit align-top bigger-125"></i>新增子类型</a>
  <a href="javascript:;" class="btn  btn-success"><i class="icon-ok align-top bigger-125"></i>编辑该类型</a>
  <a href="javascript:;" class="btn  btn-danger"><i class="icon-trash   align-top bigger-125"></i>删除该类型</a>
  </div>
  <form action="" method="post" class="form form-horizontal" id="form-user-add">
    <div class="Operate_cont clearfix">
      <label class="form-label"><span class="c-red">*</span>英文名：</label>
      <div class="formControls ">
        <input type="text" class="input-text" value="" placeholder="" id="name" name="product-category-name">
      </div>
    </div>
    <div class="Operate_cont clearfix">
      <label class="form-label"><span class="c-red">*</span>中文名：</label>
      <div class="formControls ">
        <input type="text" class="input-text" value="" placeholder="" id="zhname" name="product-category-name">
      </div>
    </div>

  </form>
  </div>
</div>
</div>
<script type="text/javascript" src="Widget/icheck/jquery.icheck.min.js"></script>
<script type="text/javascript" src="Widget/Validform/5.3.2/Validform.min.js"></script>
<script type="text/javascript" src="assets/layer/layer.js"></script>
<script type="text/javascript" src="js/H-ui.js"></script>
<script type="text/javascript" src="js/H-ui.admin.js"></script>
<script type="text/javascript">
$(function(){
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});

	$("#form-user-add").Validform({
		tiptype:2,
		callback:function(form){
			form[0].submit();
			var index = parent.layer.getFrameIndex(window.name);
			parent.$('.btn-refresh').click();
			parent.layer.close(index);
		}
	});
	//添加按钮
     $(".btn-warning").eq(0).on('click',function(){
        var name = $("#name").val();
        var zhname = $("#zhname").val();
        console.log(name);
        console.log(zhname);
        if(name && zhname){
            $.ajax({
                url:"category/categoryInsert.php",
                type:"post",
                datatype:"json",
                data:{
                   name:name,
                   zhname:zhname
                 },
                 success:function(data){
                    console.log(data);
                    layer.open({
                        content: data.msg
                    });
                    if(data.msg =="插入成功"){
                         createTree(data.data);
                    }

                 },
                  error:function(XMLHttpRequest, textStatus, errorThrown){
                     console.log(XMLHttpRequest);
                 },

            });
        }else{
            layer.open({
                title:"警告",
                content:"文本框不能为空！"
            });
        }
     });

    //编辑按钮
    $(".btn-success").eq(0).on('click',function(){
        var name = $("#name").val();
        var zhname = $("#zhname").val();
        var id = this.dataset.index;
        console.log(id);
        $.ajax({
            url:"category/categoryUpdate.php",
            type:"post",
            data:{
               name:name,
               id:id,
               zhname:zhname
             },
             success:function(data){
                console.log(data);
                layer.open({
                    content: data.msg
                });
                if(data.msg=="更新成功")
                    createTree(data.data);
             },
             error:function(XMLHttpRequest, textStatus, errorThrown){
              alert(errorThrown);
          },
             datatype:"json"
        });
     });


    //删除按钮
   $(".btn-danger").eq(0).on('click',function(){
 // jquery不行      var id = $(this).data("index");
        var id = this.dataset.index;
        console.log("id"+id);
        if(id){
            $.ajax({
                url:"category/categoryDel.php",
                type:"post",
                data:{
                   id:id
                 },
                 success:function(data){
                    console.log(data);
                    layer.open({
                        content: data.msg
                    });
                    if(data.msg !='删除失败'){
                       createTree(data.data);
                        $("#name").val("");
                        $("#zhname").val("");
                    }
                 },
                 error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.error(XMLHttpRequest);
              },
                 datatype:"json"
            });
        }else{
            layer.open({
                title:"警告",
                content:"请选择要删除的类型",
            });
        }

    });


    function createTree(zNodes) {
    	var  tree = $("#treeDemo", parent.document);
    	var ul = $("<ul ></ul>");
    	var len = zNodes.length;
    	for(let i=0;i<len;i++){
    	    var li = $("<li ><a  href='javascript:;' style='font-size:18px;margin-top:10px;' data-index='"+zNodes[i].id+"'>"+zNodes[i].zh_name+"</li>");
    	    ul.append(li);
    	}
    	tree.html(ul);
    }
});
</script>
</body>
</html>